<?php

class Emailtemplates_model extends CI_Model{

	function getall_emailtemplates()
	{
		$this->db->from('tbl_email_templates');
		$this->db->order_by("id", "asc"); 
		return $this->db->get();
	}
	
	function emailtemplate_insert($post) {
	
		$this->db->insert('tbl_email_templates',$post);
		$result = $this->db->insert_id();
		return $result;
	}
	
	function emailtemplate_record($id) {
	
		return $this->db->where_in('id',$id)->limit(1)->get('tbl_email_templates')->row_array();	
	}
	
	function emailtemplate_update_record($emailtemplate_info,$id) {
	
		 $this->db->where('id',$id);			
		 $rec = $this->db->update('tbl_email_templates',$emailtemplate_info);
		 return $rec;		 
	}
	
	function delete_emailtemplate($id)  {
		$delete_store = $this->db->query('delete from tbl_email_templates WHERE `id`='.$id);		
		return TRUE;
	}	
	
	function track_insert($post) {
				
		$row_count = $this->db->where(array('email'=>$post['email'],'newsletter_id'=>$post['newsletter_id']))->limit(1)->get('tbl_email_track')->row_array();
		$c_date = date('Y-m-d h:i:s');
		$data = array('newsletter_id'=>$post['newsletter_id'],'email'=>$post['email'],'created_date'=>$c_date);
		//if(count($row_count) == 0 )
		{
			$this->db->insert('tbl_email_track',$data);
			$result = $this->db->insert_id();
			return $result;
		}
	}
	
	function track_update($post) {
		$this->db->where(array('email'=>$post['email'],'newsletter_id'=>$post['newsletter_id']));
		$u_date = date('Y-m-d h:m:s');

		$data = array('status'=>$post['status'],'updated_date'=>$u_date);
		$result = $this->db->update('tbl_email_track',$data);
		return $result;
		
	}
	
	function subscribe()
	{		
		$result_data = $this->db->query('SELECT * FROM tbl_email_track WHERE created_date >= ( CURDATE( ) - INTERVAL 7 DAY ) LIMIT 0 , 30');
		//return $this->db->get();
		//$result = $this->db->get();
		
		$send_email = array();
		$open_email = array();
		foreach($result_data->result() as $key => $row)
		{	
			if($row->status==1)
			{		
				$send_day = date('D', strtotime($row->created_date));			
				$send_email[$send_day][] = $row;
			}
			if($row->status==0)
			{
				$open_day = date('D', strtotime($row->created_date));			
				$open_email[$open_day][] = $row->id;
			}						
		}				
		
		$send_email_id = array();
		$open_email_id = array();
		foreach($send_email as $k => $send_row)
		{
			$send_email_id[$k]=count($send_row);
		}
		foreach($open_email as $k1 => $open_row)
		{		
			$open_email_id[$k1]=count($open_row);
		}
				
		return array('send'=>$send_email_id,'open'=>$open_email_id);		
	}
	function send_mail_listing()
	{	
		$result_data = $this->db->query('SELECT * FROM tbl_email_track WHERE status=1');		
		
		return $result_data;			
	}
	function deleteSendEmail($id)
    {

	   $this->db->delete('tbl_email_track', array('id' => $id));

	 }//End of Send mail Function
	 function open_mail_listing()
	{	
		
		$result_data = $this->db->query('SELECT * FROM tbl_email_track WHERE status=0');
						
		return $result_data;			
	}
	function deleteOpenEmail($id)
	 {

	    $this->db->delete('tbl_email_track', array('id' => $id));

	 }//End of Open mail Function

	
}
